﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.IBLL;
using System.Collections.Generic;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class GoodsPSLStatistics : BasePage
    {
        public GoodsPSLStatistics()
        {
            this.ModuleNo = Module.耗材统计分析.ToString();
            this.PageOperateType = OperateType.耗材汇总统计.ToString();
        }
        HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj; 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindData_huizong();
            }
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// StringBuilder构造查询条件
        /// </summary>
        /// <returns></returns>
        string strWhere() 
        {
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(txtGoodsID.Text.Trim()))
            {
                sb.AppendFormat(@"AND CHARINDEX('{0}',GoodsID) > 0", txtGoodsID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtGoodsName.Text.Trim()))
            {
                sb.AppendFormat(@"AND CHARINDEX('{0}',GoodsName) > 0", txtGoodsName.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtOutLookC.Text.Trim()))
            {
                sb.AppendFormat(@"AND CHARINDEX('{0}',Outlookc) > 0", txtOutLookC.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtCompany_SC.Text.Trim()))
            {
                sb.AppendFormat(@"AND CHARINDEX('{0}',companyName_TB) > 0", txtCompany_SC.Text.Trim());
            }
            
            return sb.ToString();
        }

        /// <summary>
        /// 不分地区汇总后的数据
        /// </summary>
        public void BindData_huizong()
        {
            int t = 0;
            System.Data.DataTable dt = iEC.GetGoodsStatistics(1, strWhere(), this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            AspNetPager1.RecordCount = t;
            gvwGoodsTotal.PageSize = AspNetPager1.PageSize;
            this.gvwGoodsTotal.DataSource = dt;
            this.gvwGoodsTotal.DataBind();


            double PurchaseAmountTotal = 0;//采购总金额
            double DistributeAmountTotal = 0;//配送总金额
            double WareHouseAmountTotal = 0; //入库总金额
            double ReturnAmountTotal = 0;//退货总金额
            System.Data.DataTable dtSum = iEC.GetGoodsStatistics(2, strWhere(), 1, 0, out t);

            if (t > 0)
            {
                PurchaseAmountTotal = Convert.ToDouble(dtSum.Rows[0][0]);
                DistributeAmountTotal = Convert.ToDouble(dtSum.Rows[0][1]);
                WareHouseAmountTotal = Convert.ToDouble(dtSum.Rows[0][2]);
                ReturnAmountTotal = Convert.ToDouble(dtSum.Rows[0][3]);

                this.lblCount.Text = "采购总金额为：" + PurchaseAmountTotal.ToString() + "元;" + "配送总金额：" + DistributeAmountTotal.ToString() + "元;" + "入库总金额：" + WareHouseAmountTotal.ToString() + "元;" + "退货总金额：" + ReturnAmountTotal + "元;";
            }
            AddLog(string.Format("查询高值耗材汇总统计数据!"));
        }
        /// <summary>
        /// 不分地区汇总后的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData_huizong();

        }
        /// <summary>
        /// 不分地区汇总后的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void HzButton1_Click(object sender, EventArgs e)
        {
            int t = 0;
            System.Data.DataTable dt = iEC.GetGoodsStatistics(1, strWhere(), 1, 0, out t);
            AddLog(string.Format("导出高值耗材汇总统计数据成功!"));
            ExportFile.Instance.ExportData(dt, gvwGoodsTotal, this.Page);
        }

        protected void gvwGoodsTotal_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sessionId = Guid.NewGuid().ToString();
            string sessionId2 = Guid.NewGuid().ToString();


            SuperviseQuery vs = new SuperviseQuery();
            vs.ProcureCatalogID = e.CommandArgument.ToString();
            vs.BackPage = "goods";
            Session[sessionId] = vs;

            if (e.CommandName == "lbnP")
            {

                Response.Redirect(string.Format("../../Enterprise/PurchaseAmountDetail.aspx?sesid={0}&sesid2={1}", sessionId, sessionId2));
            }
            if (e.CommandName == "lbnD")
            {

                Response.Redirect(string.Format("../../Enterprise/DistributeAmountDetail.aspx?sesid={0}&sesid2={1}", sessionId, sessionId2));
            }
            if (e.CommandName == "lbnW")
            {
                Response.Redirect(string.Format("../../Enterprise/WarehouseAmountDetail.aspx?sesid={0}&sesid2={1}", sessionId, sessionId2));
            }
            if (e.CommandName == "lbnS")
            {
                Response.Redirect(string.Format("../../Enterprise/SellAmountDetail.aspx?sesid={0}&sesid2={1}", sessionId, sessionId2));
            }
            if (e.CommandName == "lbnR")
            {
                Response.Redirect(string.Format("../../Enterprise/ReturnAmountDetail.aspx?sesid={0}&sesid2={1}", sessionId, sessionId2));
            }
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
    }
}
